package com.nf.health.app.mine.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.adapter.SchemeAdapter;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.models.SchemeVo;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 个人中心  我的方案
 * Created by windoes on 2016/3/16.
 */
public class SchemeActivity extends BaseActivity {


    @Bind(R.id.scheme_listview)
    DragListView schemeListview;
    private List<SchemeVo> mlist2 = new ArrayList<SchemeVo>();


    private SchemeAdapter schemeAdapter;

    private int pageNo = 1, pageSize = 20;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_scheme);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "我的方案");


        schemeListview.setRefreshableAndLoadMoreable(true, true);

        schemeListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData("more");
            }
        });
        schemeListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();
                bundle.putString("detail", mlist2.get(position - 1).getDetail());
                bundle.putString("totaldaynum", mlist2.get(position - 1).getTotaldaynum());//共有
                bundle.putString("daynum", mlist2.get(position - 1).getDaynum());//已完成
                bundle.putString("name", mlist2.get(position - 1).getName());//名称
                bundle.putString("id", mlist2.get(position - 1).getId());//id
                bundle.putString("planids", mlist2.get(position - 1).getPlanids());//planids
                bundle.putString("everydaynum", mlist2.get(position - 1).getEverydaynum());//everydaynum
                bundle.putString("schemetempletid", mlist2.get(position - 1).getSchemetempletid());//everydaynum
                ActivityUtils.showActivity(SchemeActivity.this, HealthSchemeActivity.class, bundle);

            }
        });

    }



    //返回结果
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals("refresh")) {
            schemeListview.completeRefresh();
            List<SchemeVo> mlist = (List<SchemeVo>) object;

            mlist2.clear();
            mlist2.addAll(mlist);


            schemeAdapter = new SchemeAdapter(this, mlist2);
            schemeListview.setAdapter(schemeAdapter);
            schemeAdapter.notifyDataSetChanged();


        }else if(actionName.equals("more")){
            schemeListview.completeLoadMore();

            List<SchemeVo> mlist = (List<SchemeVo>) object;
            if(mlist.size() == 0){
                showToast("没有更多数据");
            }else{
                mlist2.addAll(mlist);
                schemeAdapter.notifyDataSetChanged();

            }

        }
    }
    protected void onResume() {
        super.onResume();
        initData("refresh");
    }

    private void initData(String flag) {
            provider.requestHealthSchemeUserList(pageNo + "", pageSize + "", flag);//请求方案列表
        }
}
